﻿using System;
using System.Net;
using System.IO;
using System.IO.IsolatedStorage;
using System.Collections.ObjectModel;
using Alexis.WindowsPhone;
using System.Xml;
using System.Xml.Serialization;

namespace Cnblogs.Service
{
    public class CacheInfo
    {
        public DateTime LastSyncTime { get; set; }

        public string LastId { get; set; }
    }

    public class CacheService
    {
        public CacheService()
        {
        }

        public static object loadCache(ServiceType type)
        {
            string path = Path.Combine(Constants.D_Data, type.ToString() + ".xml");
            using (var store = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (!store.FileExists(path))
                {
                    return null;
                }
                using (var stream = store.OpenFile(path, FileMode.Open, FileAccess.Read))
                {
                    XmlSerializer ser = new XmlSerializer(typeof(NewsFeed));
                    var obj = ser.Deserialize(stream);
                    return obj;
                }
            }
        }

        public static void saveCache(ServiceType type, NewsFeed feed)
        {
            if (feed == null || feed.Items == null || feed.Items.Length == 0)
            {
                return;
            }

            var lastInfo = PersistHelper.LoadSetttingFromStorage<CacheInfo>(type.ToString() + "_info", new CacheInfo { LastId = "", LastSyncTime = DateTime.Now });
            if (!string.IsNullOrEmpty(lastInfo.LastId))
            {
                long newId = Convert.ToInt32(feed.Items[0].Id);
                long lastId = Convert.ToInt32(lastInfo.LastId);
                //if request data is before
                if (lastId >= newId)
                {
                    return;
                }
                else
                {
                    Entry[] newItems = new Entry[20];
                    int index = 0;
                    foreach (var item in feed.Items)
                    {
                        if (Convert.ToInt32(item.Id) > lastId)
                        {
                            newItems[index] = item;
                            index++;
                        }
                    }
                    lastInfo.LastId = feed.Items[0].Id;
                    PersistHelper.SaveSettingToStorage(type.ToString() + "_info", lastInfo);
                    feed.Items = newItems;
                }
            }

            string path = Path.Combine(Constants.D_Data, type.ToString() + ".xml");
            using (var store = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (!store.DirectoryExists(Constants.D_Data))
                {
                    store.CreateDirectory(Constants.D_Data);
                }
                using (var stream = store.OpenFile(path, FileMode.Create, FileAccess.Write))
                {

                    XmlSerializer ser = new XmlSerializer(typeof(NewsFeed));
                    if (string.IsNullOrEmpty(lastInfo.LastId))
                    {
                        ser.Serialize(stream, feed);
                    }
                    else
                    {

                    }

                }
            }
        }
    }
}
